home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / x / catalog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-04  |  7.9 KB  |  328 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.
  4.  
  5.    This is part of the X user-interface for the WAIS software.  Do with it
  6.    as you please.
  7.  
  8.    jonathan@Think.COM
  9. */
  10.  
  11. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  12.  
  13.  
  14. /* this file contains X specific code - it is an integral part of XWAIS */
  15.  
  16. /* einet begin MAC */
  17. /* This was previously '#ifndef lint' */
  18. #ifdef RCSID    /* einet */
  19. /* einet end */
  20. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/x/RCS/catalog.c,v 1.3 1994/08/05 07:23:15 pfeifer Exp $";
  21. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/x/RCS/catalog.c,v 1.3 1994/08/05 07:23:15 pfeifer Exp $";
  22. #endif
  23.  
  24. #ifndef _C_CATALOG
  25. #define _C_CATALOG
  26.  
  27. #include "xwais.h"
  28. #include "cat.h"
  29.  
  30. static Boolean busy = FALSE;
  31. static long last_doc = -1;
  32. static Catbuff scat = NULL;
  33. static Widget cviewbutton, csavebutton;
  34.  
  35.                 /* begin einet */
  36. Widget parentByName(obj,name)
  37.      Widget obj;
  38.      char* name;
  39. {
  40.   while(obj != NULL &&
  41.     strcmp(XtName(obj),name) != 0) obj = XtParent(obj);
  42.   return obj;
  43. }
  44.  
  45.  
  46. Catbuff findCat(w)
  47.      Widget w;
  48. {
  49.   Catbuff cat = scat;
  50.   if (cat) 
  51.     while (cat != NULL && cat->shell !=  parentByName(w,"catpopup"))
  52.       cat = (Catbuff) cat->nextCat;
  53.   return cat;
  54. }
  55.  
  56. int freeCat(cat)
  57.      Catbuff cat;
  58. {
  59.   if (cat->Items) free(cat->Items);
  60.   if (cat->Types) free(cat->Types);
  61.   if (cat->headlines) free(cat->headlines);
  62.   if (cat->Docids) free(cat->Docids);
  63.   free(cat);
  64. }
  65.  
  66.  
  67. int killCat(w)
  68.      Widget w;
  69. {
  70.   Catbuff lastCat = NULL;
  71.   Catbuff cat = scat;
  72.   if (cat) { 
  73.     while (cat != NULL && cat->shell !=  parentByName(w,"catpopup")) {
  74.       lastCat = cat;
  75.       cat = (Catbuff) cat->nextCat;
  76.     }
  77.     if (cat == scat) {
  78.       scat = (Catbuff) cat->nextCat;
  79.       freeCat(cat);
  80.     } else {
  81.       lastCat->nextCat = cat->nextCat;
  82.       freeCat(cat);
  83.     }
  84.     return 1;
  85.   }
  86.   return 0;
  87. }
  88.                 /* end einet */
  89.  
  90.  
  91. Catbuff build_cat(catalog, source)
  92. char* catalog;
  93. SourceID source;
  94. {
  95.   char database[STRINGSIZE], headline[STRINGSIZE], docid[STRINGSIZE];
  96.   char* p = catalog;
  97.   long i, j, numdocs = 0, start, end, size;
  98.   DocumentID docID;
  99.   DocList dl;
  100.   Catbuff result;
  101. /*
  102.   extern char *strstr(), *index();
  103. */
  104.   if((result = (Catbuff)s_malloc(sizeof(_Catbuff))) == NULL) {
  105.     PrintStatus(STATUS_URGENT, STATUS_HIGH, BADALLOC_MESSAGE);
  106.     return NULL;
  107.   }
  108.  
  109.   /* get the database name */
  110.   p = strstr(p, "Catalog for database: ");
  111.   if (p == NULL) {
  112.     freeCat(result);
  113.     return NULL;
  114.   }
  115.   p += strlen("Catalog for database: ");
  116.   memset(database, 0, STRINGSIZE);
  117.   strncpy(database, p, strstr(p, "\n")-p);
  118.   while(numdocs == 0 && p != NULL && *p != 0) {
  119.     sscanf(p, "%d total documents", &numdocs);
  120.     p = strstr(p, "\n")+1;
  121.   }
  122.  
  123.   result->source = source;
  124.   result->database = s_strdup(database);
  125.   result->Items = (String*)s_malloc((numdocs+1)*sizeof(String));
  126.   result->Types = (String*)s_malloc((numdocs+1)*sizeof(String));
  127.   result->headlines = (String*)s_malloc((numdocs+1)*sizeof(String));
  128.   result->Docids = (String*)s_malloc((numdocs+1)*sizeof(String));
  129.   result->numDocs = numdocs;
  130.  
  131.   /* find the first document */
  132.   p = strstr(p, "\nDocument # ");
  133.  
  134.   for(i = 0; i < numdocs; i++) {
  135.     char *s, *s2;
  136.     if (p == NULL) {        /* premature end of catalog */
  137.       freeCat(result);
  138.       return NULL;
  139.     }
  140.     /* look for type string */
  141.     s = index(p+1,'\n');
  142.     if (s) {
  143.       *s = '\0';        /* temporarily terminate the sub-string */
  144.       if (s2 = strstr(p,"Type: ")) {
  145.     /* found a type string */
  146.     result->Types[i] = s_strdup(s2+strlen("Type: "));
  147.       } else 
  148.     result->Types[i] = s_strdup("TEXT");
  149.       *s = '\n';        
  150.     }
  151.  
  152.     /* set headline */
  153.     p = strstr(p, "\nHeadline: ");
  154.     if(p != NULL)
  155.       p++; 
  156.     else {
  157.       freeCat(result);
  158.       return (NULL);
  159.     }
  160.     p += strlen("Headline: ");
  161.     memset(headline, 0, STRINGSIZE);
  162.     strncpy(headline, p, index(p, '\n')-p);
  163.     result->headlines[i] = s_strdup(headline);
  164.  
  165.     /* set the docid */
  166.     p = index(p, '\n');
  167.     if(p == NULL) break;
  168.     else p += 1+strlen("DocID: ");
  169.     memset(docid, 0, STRINGSIZE);
  170.     strncpy(docid, p, index(p, '\n')-p);
  171.     result->Docids[i] = s_strdup(docid);
  172.  
  173.     /* build the printable headline */
  174.     sscanf(docid, "%d %d", &start, &end);
  175.     size = end - start;
  176.     if (size > 1024) 
  177.       sprintf(headline, "%4.1fK %s", size/1024.0, result->headlines[i]);
  178.     else
  179.       sprintf(headline, "%5d %s", size, result->headlines[i]);
  180.     result->Items[i] = s_strdup(headline);
  181.       
  182.     /* go to the next catalog record */
  183.     for(j = 0; j < 2; j++) {
  184.       p = index(p, '\n');
  185.       if(p == NULL) break;
  186.       else p++;
  187.     }
  188.   }
  189.   result->nextCat = (void*) scat;
  190.   scat = result;
  191.   return result;
  192. }
  193.   
  194.  
  195. static DocumentID
  196. makecatdoc(cat, i)
  197.      Catbuff cat;
  198.      long i;
  199. {
  200.   DocumentID docID = NULL;
  201.   char* type;
  202.  
  203.   if((docID = (DocumentID)s_malloc(sizeof(_DocumentID))) != NULL) {
  204.     docID->rawScore = 1000;
  205.     docID->start = docID->end = -1;
  206.     if((docID->doc = (CRetDocument)s_malloc(sizeof(_CRetDocument))) != NULL) {
  207.       docID->doc->sourceID = cat->source;
  208.       docID->doc->numLines = 0;
  209.       docID->doc->numChars = -1;
  210.       docID->doc->best = 0;
  211.       docID->doc->headline = s_strdup(cat->headlines[i]);
  212.       docID->doc->type = (char **)s_malloc(2*sizeof(char*));
  213.       docID->doc->type[0] = cat->Types[i];
  214.       docID->doc->type[1] = NULL;
  215.  
  216.       docID->doc->id = (DocID*)s_malloc(sizeof(DocID));
  217.       memset(docID->doc->id, 0, sizeof(DocID));
  218.       docID->doc->id->originalDatabase = stringToAny(cat->database);
  219.       docID->doc->id->originalLocalID = stringToAny(cat->Docids[i]);
  220.     }
  221.   }
  222.   return docID;
  223. }
  224.  
  225. /* ARGSUSED */
  226. static void
  227. ViewCat(w, closure, call_data)
  228.      Widget w;
  229.      XtPointer closure, call_data;
  230. {
  231.   Catbuff cat = findCat(w);    /* einet */
  232.   static int document_number;
  233.  
  234.   double_click = FALSE;
  235.   LastClicked = w;
  236.  
  237.   if(!busy)
  238.     if((document_number = get_selected_item(cat->List->ListWidget)) ==
  239.        NO_ITEM_SELECTED)
  240.       PrintStatus(STATUS_URGENT, STATUS_HIGH, NOSELECT_MESSAGE);
  241.     else {
  242.       DocumentID doc;
  243.  
  244.       if (document_number != last_doc) {
  245.     last_doc = document_number;
  246.       }
  247.       else {
  248.     messwidget = cat->status;
  249.     if((doc = makecatdoc(cat, document_number)) == NULL)
  250.       PrintStatus(STATUS_URGENT, STATUS_HIGH, NODOC_MESSAGE);
  251.     else {
  252.       ViewDoc(doc, doc->doc->type[0], 0, (w == csavebutton));
  253.     }
  254.       }
  255.     }
  256. }
  257.  
  258. static void
  259. CloseCat(w, closure, call_data)
  260.      Widget w;
  261.      XtPointer closure, call_data;
  262. {
  263.   XtPopdown(parentByName(w,"catpopup"));
  264.   killCat(w);        /* einet */
  265. }
  266.  
  267. Widget
  268. MakeCatPopup(parent, cat, name)
  269.      Widget parent;
  270.      Catbuff cat;
  271.      char *name;
  272. {
  273.   Widget frame, button, stringlabelwid, view;
  274.   Arg args[TWO];
  275.   static String items[] = {NULL};
  276.  
  277.   XtSetArg(args[ZERO], XtNtitle, name);
  278.   XtSetArg(args[ONE], XtNiconName, name);
  279.   cat->shell = XtCreatePopupShell("catpopup", applicationShellWidgetClass,
  280.                   parent, args, TWO);
  281.   frame =
  282.     XtCreateManagedWidget("questionpopupform", formWidgetClass,
  283.               cat->shell, NULL, ZERO);
  284.  
  285.   stringlabelwid = MakeLabel(frame, "foo", "Catalog of", NULL, NULL);
  286.  
  287.   stringlabelwid =
  288.     MakeLabel(frame, "qreslabel", "documents:",
  289.           stringlabelwid, NULL);
  290.  
  291.   cviewbutton = button = 
  292.     MakeCommandButton(frame, "view", ViewCat,
  293.               stringlabelwid, NULL, NULL);
  294.  
  295.   csavebutton = button =
  296.     MakeCommandButton(frame, "qsave", ViewCat,
  297.               button, NULL, NULL);
  298.  
  299.   button =
  300.     MakeCommandButton(frame, "qdone", CloseCat,
  301.               button, NULL, cat);
  302.  
  303.   XtSetArg (args[ZERO], XtNallowVert, True);
  304.   XtSetArg (args[ONE], XtNfromVert, NULL);
  305.  
  306.   view = XtCreateManagedWidget ("rview", viewportWidgetClass,
  307.                 frame, args, TWO);
  308.  
  309.   cat->List = 
  310.     MakeScrollList(view, "catresults", cat->Items,
  311.            ViewCat, NULL, NULL);
  312.  
  313.   stringlabelwid =
  314.     MakeLabel(frame, "statuslabel", "Status:",
  315.           NULL, NULL);
  316.  
  317.   XtSetArg(args[ZERO], XtNeditType, XawtextEdit);
  318.  
  319.   cat->status =
  320.     XtCreateManagedWidget("statusWindow", asciiTextWidgetClass,
  321.               frame, args, ONE);
  322.  
  323.   return(cat->shell);
  324. }
  325.  
  326. #endif
  327.  
  328.